# -*- coding: utf-8 -*-
# @Author   : niejiali
# @Time     : 2019/1/16 8:52
# @File     : ginger.py
# @Software : PyCharm
from werkzeug.exceptions import HTTPException

from app import create_app
from app.lib.error import APIException
from app.lib.error_code import ServerError

app = create_app()


# 全局异常处理
@app.errorhandler(Exception)
def framework_error(e):
    # flask 1.0
    # 0.12
    # APIException
    # HTTPException
    # Exception
    if isinstance(e, APIException):
        return e
    if isinstance(e, HTTPException):
        code = e.code
        msg = e.description
        error_code = 1007
        return APIException(msg, code, error_code)
    else:
        # log
        if not app.config['DEBUG']:
            return ServerError()
        else:
            raise e


if __name__ == '__main__':
    app.run(debug=1)